<template>
  <div class="login-container">
    <div class="login-box" :class="{ 'active': isActive }">
      <div class="login-header">
        <h2>后台管理系统</h2>
        <p>请输入您的账号和密码</p>
      </div>

      <form @submit.prevent="handleSubmit">
        <div class="input-group" :class="{ 'focus': usernameFocus || form.username }">
          <input type="text" v-model="form.username" @focus="usernameFocus = true" @blur="handleBlur('username')"
            required>
          <label>用户名</label>
          <span class="highlight"></span>
          <span class="bar"></span>
          <i class="icon-user"></i>
        </div>

        <div class="input-group" :class="{ 'focus': passwordFocus || form.password }">
          <input :type="showPassword ? 'text' : 'password'" v-model="form.password" @focus="passwordFocus = true"
            @blur="handleBlur('password')" required>
          <label>密码</label>
          <span class="highlight"></span>
          <span class="bar"></span>
          <i class="icon-lock"></i>
          <span class="toggle-password" @click="showPassword = !showPassword">
            {{ showPassword ? '👁️' : '👁️‍🗨️' }}
          </span>
        </div>

        <div class="remember-me">
          <input type="checkbox" id="remember" v-model="rememberMe">
          <label for="remember">记住我</label>
        </div>

        <button type="submit" class="login-btn" :disabled="loading">
          <span v-if="!loading">登 录</span>
          <span v-else class="loading">
            <i class="spinner"></i> 登录中...
          </span>
        </button>
      </form>

      <div class="footer-links">
        <!-- <a href="#">忘记密码?</a> -->
        <!-- <a href="#">注册账号</a> -->
      </div>
    </div>

    <div class="particles" id="particles-js"></div>
  </div>
</template>
  
<script>
import md5 from 'md5';
export default {
  name: 'LoginPage',
  data() {
    return {
      isActive: false,
      form: {
        username: '',
        password: ''
      },
      usernameFocus: false,
      passwordFocus: false,
      showPassword: false,
      rememberMe: false,
      loading: false
    }
  },
  mounted() {
    this.rememberMe = localStorage.getItem("rememberMe") == 1;
    this.isActive = true;

    this.initUserInfo();
    this.initParticles();
  },
  watch: {
    rememberMe(nv) {
      localStorage.setItem("rememberMe", Number(nv));
    }
  },
  methods: {
    handleBlur(field) {
      if (field === 'username') {
        this.usernameFocus = false;
      } else {
        this.passwordFocus = false;
      }
    },
    initUserInfo() {
      if (this.rememberMe) {
        this.form.username = localStorage.getItem("username");
        this.form.password = localStorage.getItem("password");
      }
    },
    handleSubmit() {
      if (!this.form.username || !this.form.password) {
        this.$message.error('请输入用户名和密码');
        return;
      }

      this.loading = true;
      let param = {
        username: this.form.username,
        password: this.form.password.length == 32 ? this.form.password : md5(this.form.password)
      }

      this.$store.dispatch("Login", param).then(res => {
        console.log(res)
        if (res.code == 200) {
          this.$message.success("登录成功");
          this.$router.push({ name: "home" });
        }
        this.loading = false;
      }).catch(err => {
        this.$message.error("登录失败");
        this.loading = false;
      });

    },
    initParticles() {
      if (typeof window !== 'undefined' && window.particlesJS) {
        particlesJS('particles-js', {
          particles: {
            number: { value: 80, density: { enable: true, value_area: 800 } },
            color: { value: "#ffffff" },
            shape: { type: "circle" },
            opacity: { value: 0.5, random: true },
            size: { value: 3, random: true },
            line_linked: { enable: true, distance: 150, color: "#ffffff", opacity: 0.4, width: 1 },
            move: { enable: true, speed: 2, direction: "none", random: true, straight: false, out_mode: "out" }
          },
          interactivity: {
            detect_on: "canvas",
            events: {
              onhover: { enable: true, mode: "repulse" },
              onclick: { enable: true, mode: "push" }
            }
          }
        });
      }
    },

  }
}
</script>
  
<style scoped>
/* 放大字体版本 */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Arial', sans-serif;
}

.login-container {
  position: relative;
  width: 100%;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-image: url('../../assets/img/bg/bg.png');
  background-size: 100% 100%;
  overflow: hidden;
}

.particles {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
}

.login-box {
  position: relative;
  width: 450px;
  /* 放大容器 */
  padding: 50px;
  /* 增加内边距 */
  background: rgba(255, 255, 255, 0.9);
  border-radius: 15px;
  /* 更大的圆角 */
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.25);
  /* 更强的阴影 */
  z-index: 10;
  opacity: 0;
  transform: translateY(50px);
  transition: all 0.5s ease;
}

.login-box.active {
  opacity: 1;
  transform: translateY(0);
}

.login-header {
  text-align: center;
  margin-bottom: 50px;
  /* 增加间距 */
}

.login-header h2 {
  color: #333;
  font-size: 32px;
  /* 放大标题 */
  margin-bottom: 15px;
  font-weight: 600;
}

.login-header p {
  color: #666;
  font-size: 16px;
  /* 放大副标题 */
}

/* 输入框样式 - 放大版本 */
.input-group {
  position: relative;
  margin-bottom: 40px;
  /* 增加间距 */
}

.input-group input {
  width: 100%;
  padding: 15px 15px 15px 50px;
  /* 更大的内边距 */
  font-size: 18px;
  /* 更大的字体 */
  color: #333;
  border: none;
  border-bottom: 2px solid #999;
  /* 更粗的边框 */
  outline: none;
  background: transparent;
  transition: all 0.3s ease;
}

.input-group label {
  position: absolute;
  top: 15px;
  left: 50px;
  font-size: 18px;
  /* 更大的标签字体 */
  color: #999;
  pointer-events: none;
  transition: all 0.3s ease;
}

.input-group .highlight {
  position: absolute;
  height: 3px;
  /* 更粗的高光线 */
  width: 0;
  bottom: 0;
  left: 0;
  background: #667eea;
  transition: all 0.4s ease;
}

.input-group .bar {
  position: absolute;
  height: 2px;
  /* 更粗的底边线 */
  width: 100%;
  bottom: 0;
  /* background: #ddd; */
}

.input-group i {
  position: absolute;
  left: 15px;
  top: 15px;
  color: #999;
  font-size: 22px;
  /* 更大的图标 */
}

/* 输入框聚焦效果 */
.input-group.focus label,
.input-group input:valid+label {
  top: -25px;
  left: 0;
  font-size: 14px;
  color: #667eea;
}

.input-group.focus .highlight,
.input-group input:valid~.highlight {
  width: 100%;
}

.input-group.focus i,
.input-group input:valid~i {
  color: #667eea;
}

/* 密码显示切换 */
.toggle-password {
  position: absolute;
  right: 15px;
  top: 15px;
  cursor: pointer;
  user-select: none;
  font-size: 20px;
  /* 更大的眼睛图标 */
}

/* 记住我 */
.remember-me {
  display: flex;
  align-items: center;
  margin-bottom: 30px;
  /* 增加间距 */
  font-size: 16px;
  /* 更大的字体 */
}

.remember-me input {
  margin-right: 10px;
  transform: scale(1.3);
  /* 更大的复选框 */
}

/* 登录按钮 */
.login-btn {
  width: 100%;
  padding: 16px;
  /* 更大的按钮 */
  background: linear-gradient(to right, #667eea, #764ba2);
  border: none;
  border-radius: 30px;
  /* 更大的圆角 */
  color: white;
  font-size: 18px;
  /* 更大的字体 */
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 6px 20px rgba(102, 126, 234, 0.5);
  /* 更强的阴影 */
  font-weight: 500;
}

.login-btn:hover {
  transform: translateY(-3px);
  box-shadow: 0 8px 25px rgba(102, 126, 234, 0.7);
}

.login-btn:disabled {
  background: #ccc;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

/* 加载动画 */
.loading {
  display: flex;
  align-items: center;
  justify-content: center;
}

.spinner {
  width: 22px;
  height: 22px;
  margin-right: 10px;
  border: 3px solid rgba(255, 255, 255, 0.3);
  border-radius: 50%;
  border-top-color: white;
  animation: spin 1s ease-in-out infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* 底部链接 */
.footer-links {
  display: flex;
  justify-content: space-between;
  margin-top: 30px;
  /* 增加间距 */
}

.footer-links a {
  color: #666;
  text-decoration: none;
  font-size: 16px;
  /* 更大的字体 */
  transition: color 0.3s ease;
  font-weight: 500;
}

.footer-links a:hover {
  color: #667eea;
  text-decoration: underline;
}
</style>